import 'package:flutter/material.dart';

/// 2022年更新  适配 flutter 3.0
class ClipWrapHome extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ClipWrapHomeState();
  }
}

class ClipWrapHomeState extends State {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Chip And Wrap"),
      ),
      body: Padding(
        padding: EdgeInsets.all(20),
        child: Wrap(
          spacing: 12,
          runSpacing: 10,
          children: buildWrapChildren(),
        ),
      ),
    );
  }

  final List<String> _tagsList = ["早睡", "早起", "测试", "努力", "不想睡觉", "清晨的太阳"];

  buildWrapChildren() {
    List<Widget> itemChildList = [];
    //根据标签创建Widget
    for (String value in _tagsList) {

      itemChildList.add(
        Chip(
          label: Text(value),
          deleteIcon: Icon(Icons.close),
          deleteIconColor: Colors.red,
          onDeleted: () {
            //删除当当显示的 Chip
            _tagsList.removeWhere((element) => element == value);
            //更新当前视图
            setState(() {

            });
          },
        ),
      );
    }
    return itemChildList;
  }
}
